

//
//  CASpringAnimationController.m
//  界面动画集
//
//  Created by YYY on 2017/7/3.
//  Copyright © 2017年 成品家（北京）网路科技有限公司. All rights reserved.
//

#import "CASpringAnimationController.h"

//弹簧动画是iOS9之后有的
@interface CASpringAnimationController ()

@property(nonatomic,strong)UIImageView *pearImageView;

@end

@implementation CASpringAnimationController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"CASpringAnimation";
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"弹簧动画" style:UIBarButtonItemStylePlain target:self action:@selector(springAnimation)];
    [self.view addSubview:self.pearImageView];
    // Do any additional setup after loading the view.
}

-(void)springAnimation
{
    CASpringAnimation *springAnimation = [CASpringAnimation animationWithKeyPath:@"transform.rotation"];
    //质量，影响图层运动时的弹簧惯性，质量越大，弹簧拉伸和压缩的幅度越大
    springAnimation.mass = 10;
    //刚度系数(劲度系数/弹性系数)，刚度系数越大，形变产生的力就越大，运动越快
    springAnimation.stiffness = 5000;
    //阻尼系数，阻止弹簧伸缩的系数，阻尼系数越大，停止越快
    springAnimation.damping = 100;
    //初始速率，动画视图的初始速度大小;速率为正数时，速度方向与运动方向一致，速率为负数时，速度方向与运动方向相反
    springAnimation.initialVelocity = 5;
    springAnimation.duration = 5;
    springAnimation.fromValue = @(M_PI);
    springAnimation.toValue = @(M_PI/2);
    springAnimation.removedOnCompletion = NO;
    springAnimation.fillMode = kCAFillModeForwards;
    springAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    [self.pearImageView.layer addAnimation:springAnimation forKey:nil];
    
    
}
-(UIImageView *)pearImageView
{
    if (!_pearImageView)
    {
        _pearImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"pear"]];
        //_pearImageView.center = self.view.center;
        _pearImageView.frame = CGRectMake(50, 200, 100, 100);
    }
    return  _pearImageView;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
